package com.guit.hrms.controller;

import com.guit.hrms.dto.MatterDTO;
import com.guit.hrms.po.Matter;
import com.guit.hrms.po.utilsPO.Result;
import com.guit.hrms.service.MatterService;
import com.guit.hrms.utils.ThreadLocalUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@Slf4j
@CrossOrigin
@RestController
@RequestMapping("/api/todoList")
public class MatterController {
    @Autowired
    private MatterService matterService;

    /**
     * 查询员工个人待办事项
     * @return 待办事项列表
     */
    @GetMapping
    public Result queryMatter(){
        Map<String,Object> map = ThreadLocalUtil.get();
        String employeeId = (String) map.get("employeeId");
        List<Matter> list = matterService.queryMatter(employeeId);
        return Result.success(list);
    }

    /**
     * 添加待办事项
     * @param matterDTO 待办事项信息
     * @return 添加结果
     */
    @PostMapping
    public Result addMatter(@RequestBody MatterDTO matterDTO){
        Integer result = matterService.addMatter(matterDTO);
        if(result == 1){
            return Result.success("添加成功");
        } else {
            return Result.error("添加失败");
        }
    }

    /**
     * 修改待办事项
     * @param matterDTO 待办事项信息
     * @return 修改结果
     */
    @PatchMapping
    public Result updateMatter(@RequestBody MatterDTO matterDTO){
        Integer result = matterService.updateMatter(matterDTO);
        if(result == 1){
            return Result.success("修改成功");
        } else {
            return Result.error("修改失败");
        }
    }

    /**
     * 完成待办事项
     * @param id 待办事项id
     * @return 处理结果
     */
    @PatchMapping("/finish/{id}")
    public Result finishMatter(@PathVariable Integer id){
        Integer result = matterService.finishMatter(id);
        if(result == 1){
            return Result.success("处理成功");
        } else {
            return Result.error("处理失败");
        }
    }
}
